//
//  TRViewMaker.m
//  TRView
//
//  Created by cry on 2017/8/25.
//  Copyright © 2017年 cry. All rights reserved.
//

#import "TRViewMaker.h"
#import "Masonry.h"

static NSString *const TRColor = @"color";
static NSString *const TRText = @"text";
static NSString *const TRImage = @"image";
static NSString *const TRAxis = @"axis";

static NSString *const TRHeight = @"height";
static NSString *const TRWidth = @"width";
static NSString *const TRTop = @"top";
static NSString *const TRLeft = @"left";
static NSString *const TRRight = @"right";
static NSString *const TRBottom = @"bottom";

@interface TRViewMaker()

@property (nonatomic, strong) NSMutableArray<TRView *> *viewManager;
@property (nonatomic, strong) UIView *view;
@property (nonatomic, assign) TRLayoutType axisType;

@end

@interface TRView()

@property (nonatomic, assign) TRViewType viewType;
@property (nonatomic, assign) TRLayoutType layoutType;
@property (nonatomic, strong) NSMutableDictionary *attribute;
@property (nonatomic, copy)   TRViewContainBlock containBlock;

- (UIView *)generateView;

@end

@implementation TRViewMaker

+ (TRViewMaker *)maker{
    return [[TRViewMaker alloc] init];
}

- (TRView *(^)(TRViewType))addView{
    
    return ^(TRViewType viewType){
        return [self addViewWithType:viewType];
    };
}

- (TRView *)addSoild{
    return [self addViewWithType:TRViewTypeSoild];
}

- (UIView *(^)())initView{
    return ^(){
        if (!self.view) {
            self.view = [[UIView alloc] init];
        }
        UIView *view = self.view;
        __block UIView *lastView = nil;
        [self.viewManager enumerateObjectsUsingBlock:^(TRView   * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            UIView *singleView = [obj generateView];
            [view addSubview:singleView];
            // set up layout
            NSMutableDictionary *attribute = obj.attribute;
            NSNumber *height = attribute[TRHeight]?:@(0);
            NSNumber *width = attribute[TRWidth]?:@(0);
            NSNumber *left = attribute[TRLeft]?:@(0);
            NSNumber *top = attribute[TRTop]?:@(0);
            NSNumber *right = attribute[TRRight]?:@(0);
            NSNumber *bottom = attribute[TRBottom]?:@(0);
            if (self.axisType == TRLayoutTypeHorizontal) {
                [singleView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(top);
                    make.bottom.mas_offset(bottom);
                    make.left.mas_equalTo(lastView?lastView.mas_right:view).offset(left.doubleValue);
                    make.width.mas_equalTo(width);
                }];
            }else{
                [singleView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(left);
                    make.right.mas_offset(right);
                    make.top.mas_equalTo(lastView?lastView.mas_bottom:view).offset(top.doubleValue);
                    make.height.mas_equalTo(height);
                }];
            }
            lastView = singleView;
        }];
        return view;
    };
}
- (TRView *)addViewWithType:(TRViewType)viewType{
    TRView *view = [[TRView alloc] init];
    view.viewType = viewType;
    [self.viewManager addObject:view];
    return view;
}

- (NSMutableArray<TRView *> *)viewManager{
    if (!_viewManager) {
        _viewManager = [NSMutableArray array];
    }
    return _viewManager;
}

@end

@implementation TRView

- (TRView *(^)(NSString *))text{
    return ^(NSString *text){
        [self.attribute setValue:text forKey:TRText];
        return self;
    };
}
- (TRView *(^)(UIColor *))color{
    return ^(UIColor *color){
        [self.attribute setValue:color forKey:TRColor];
        return self;
    };
}
- (TRView *(^)(UIImage *))image{
    return ^(UIImage *image){
        [self.attribute setValue:image forKey:TRImage];
        return self;
    };
}
- (TRView *(^)(CGFloat))height{
    return ^(CGFloat height){
        [self.attribute setValue:@(height) forKey:TRHeight];
        return self;
    };
}

- (TRView *(^)(CGFloat ))width{
    return ^(CGFloat width){
        [self.attribute setValue:@(width) forKey:TRWidth];
        return self;
    };
}
- (TRView *(^)(CGFloat ))left{
    return ^(CGFloat left){
        [self.attribute setValue:@(left) forKey:TRLeft];
        return self;
    };
}
- (TRView *(^)(CGFloat ))top{
    return ^(CGFloat top){
        [self.attribute setValue:@(top) forKey:TRTop];
        return self;
    };
}
- (TRView *(^)(CGFloat ))right{
    return ^(CGFloat right){
        [self.attribute setValue:@(right) forKey:TRRight];
        return self;
    };
}
- (TRView *(^)(CGFloat ))bottom{
    return ^(CGFloat bottom){
        [self.attribute setValue:@(bottom) forKey:TRBottom];
        return self;
    };
}

- (TRView *)contains:(TRViewContainBlock)block{
    self.containBlock = block;
    return self;
}

- (TRView *)axisHorizontalContains:(TRViewContainBlock)block{
    self.layoutType = TRLayoutTypeHorizontal;
    return [self contains:block];
}
- (TRView *)axisVerticalContains:(TRViewContainBlock)block{
    self.layoutType = TRLayoutTypeVertical;
    return [self contains:block];
}

- (NSMutableDictionary *)attribute{
    if (_attribute == nil) {
        _attribute = [NSMutableDictionary dictionary];
    }
    return _attribute;
}

#pragma mark - private method
- (UIView *)generateView{
    
    switch (self.viewType) {
        case TRViewTypeSoild:{
            UIView *view = [[UIView alloc] init];
            NSMutableDictionary *attribute = self.attribute;
            TRViewContainBlock block = self.containBlock;
            if (attribute.count == 0) {
                return view;
            }
            UIColor *color = attribute[TRColor];
            if (color) {
                view.backgroundColor = color;
            }
            if (block) {
                TRViewMaker *maker = [TRViewMaker maker];
                maker.view = view;
                maker.axisType = self.layoutType;
                block(maker);
                maker.initView();
            }
            return view;
        }break;
        case TRViewTypeText:{
            UILabel *label = [[UILabel alloc] init];
            NSMutableDictionary *attribute = self.attribute;
            if (attribute.count == 0) {
                return label;
            }
            UIColor *color = attribute[TRColor];
            if (color) {
                label.textColor = color;
            }
            NSString *text = attribute[TRText];
            if (text) {
                label.text = text;
            }
            return label;
        }break;
        case TRViewTypeImage:{
            UIImageView *imageView = [[UIImageView alloc] init];
            NSMutableDictionary *attribute = self.attribute;
            if (attribute.count == 0) {
                return imageView;
            }
            UIImage *image = attribute[TRImage];
            if (image) {
                imageView.image = image;
            }
            return imageView;
        }break;
            
        default:{
            return [[UIView alloc] init];
        }break;
    }
    return nil;
}

@end
